﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MovieRecorder.CApMRCRecorderSettings
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Windows.Media;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.MovieRecorder;

[Serializable]
public class CApMRCRecorderSettings
{
  private string m_fileFormatPattern = "{0}{1}.{2}";
  private string m_fileNameFormatPattern = "Movie_{0}_{1}";
  private const string DEFAULT_FILE_FORMAT = "mp4";
  private string m_fileFullName;
  private string m_screenCaptureOption;
  private IList<KeyValuePair<string, string>> m_screenCaptureOptions;
  private const string SCREEN_CAPTURE_APPLICATION_KEY = "Application";
  private const string SCREEN_CAPTURE_FULL_SCREEN_KEY = "Full Screen";

  public CApMRCRecorderSettings()
  {
    this.FileFormat = "mp4";
    this.FileDestination = Path.GetTempPath();
    this.m_fileFullName = string.Empty;
    this.ScreenCaptureArea = CaptureArea.GraphicViewerStandard;
    this.InitScreenCaptureOptions();
    this.m_screenCaptureOption = "Application";
    this.ResolutionWidth = 1920U;
    this.ResolutionHeight = 1080U;
    this.SelectedResolution = -1;
    this.FrameRate = 20U;
    this.Compression = 46U;
    this.Codec = (TxVideoCodec) 28;
    this.TimeSource = (TxMovieTimeSource) 1;
    this.ChangeSimulationTimeInterval = true;
    this.OverlayText = string.Empty;
    this.OverlayFontSize = (short) 24;
    this.OverlayFontColor = Colors.White;
    this.OverlayFontShadowColor = Colors.Black;
    this.OverlayImagePath = string.Empty;
    this.OverlayImageWidth = 0U;
    this.OverlayImageHeight = 0U;
    this.OverlayImageTransparency = 0U;
    this.OverlayImageSizePercent = 20U;
    this.OverlayPosition = (TxMovieOverlayPosition) 8;
    this.SceneSettings = new CApMRCMovieRecorderSceneSettings();
    this.ExpanderSettings = new CApMRCMovieRecorderSettingsExpanders();
  }

  public CApMRCRecorderSettings(CApMRCRecorderSettings settings)
  {
    this.m_fileFullName = string.Empty;
    this.InitScreenCaptureOptions();
    this.FileFormat = settings.FileFormat;
    this.FileName = settings.FileName;
    this.FileDestination = settings.FileDestination;
    this.FrameRate = settings.FrameRate;
    this.Compression = settings.Compression;
    this.ResolutionWidth = settings.ResolutionWidth;
    this.ResolutionHeight = settings.ResolutionHeight;
    this.SelectedResolution = settings.SelectedResolution;
    this.ScreenCaptureArea = settings.ScreenCaptureArea;
    this.SelectedScreenCaptureOption = settings.SelectedScreenCaptureOption;
    this.Codec = settings.Codec;
    this.TimeSource = settings.TimeSource;
    this.ChangeSimulationTimeInterval = settings.ChangeSimulationTimeInterval;
    this.OverlayText = settings.OverlayText;
    this.OverlayFontSize = settings.OverlayFontSize;
    this.OverlayFontColor = settings.OverlayFontColor;
    this.OverlayFontShadowColor = settings.OverlayFontShadowColor;
    this.OverlayImagePath = settings.OverlayImagePath;
    this.OverlayImageWidth = settings.OverlayImageWidth;
    this.OverlayImageHeight = settings.OverlayImageHeight;
    this.OverlayImageSizePercent = settings.OverlayImageSizePercent;
    this.OverlayImageTransparency = settings.OverlayImageTransparency;
    this.OverlayPosition = settings.OverlayPosition;
    this.SceneSettings = new CApMRCMovieRecorderSceneSettings(settings.SceneSettings);
    this.ExpanderSettings = new CApMRCMovieRecorderSettingsExpanders(settings.ExpanderSettings);
  }

  public uint FrameRate { get; set; }

  public uint Compression { get; set; }

  public string FileFormat { get; set; }

  public string FileDestination { get; set; }

  public string FileName { get; set; }

  private string FileNamePrefix
  {
    get
    {
      string input = TxApplication.ActiveDocument.CurrentStudy != null ? ((ITxObject) TxApplication.ActiveDocument.CurrentStudy).Name : "Study";
      return new Regex($"[{Regex.Escape(new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()))}]").Replace(input, "");
    }
  }

  public CaptureArea ScreenCaptureArea { get; set; }

  public string SelectedScreenCaptureOption
  {
    get => this.m_screenCaptureOption;
    set
    {
      if (!(this.m_screenCaptureOptions.FirstOrDefault<KeyValuePair<string, string>>((Func<KeyValuePair<string, string>, bool>) (x => x.Key == value)).Key == value))
        return;
      this.m_screenCaptureOption = value;
    }
  }

  public uint ResolutionWidth { get; set; }

  public uint ResolutionHeight { get; set; }

  public int SelectedResolution { get; set; }

  public CApMRCMovieRecorderSceneSettings SceneSettings { get; set; }

  public CApMRCMovieRecorderSettingsExpanders ExpanderSettings { get; set; }

  public bool IsApplicationCaptureMode()
  {
    return this.ScreenCaptureArea == CaptureArea.Screen && this.SelectedScreenCaptureOption == "Application";
  }

  public string GetFileFullName(bool generate)
  {
    string str1;
    string str2;
    if (!string.IsNullOrEmpty(this.FileName))
    {
      str1 = this.FileName;
      str2 = this.FileName + "*";
    }
    else
    {
      str1 = string.Format(this.m_fileNameFormatPattern, (object) this.FileNamePrefix, (object) 1);
      str2 = string.Format(this.m_fileNameFormatPattern, (object) this.FileNamePrefix, (object) "*");
    }
    if (generate)
    {
      string path = CApMRCRecorderUtilities.NormalizeFilePath(this.FileDestination);
      if (Directory.Exists(path) && !string.IsNullOrEmpty(this.FileFormat))
      {
        string searchPattern = string.Format(this.m_fileFormatPattern, (object) string.Empty, (object) str2, (object) this.FileFormat);
        IEnumerable<string> source = Directory.EnumerateFiles(path, searchPattern, SearchOption.TopDirectoryOnly).Select<string, string>(new Func<string, string>(Path.GetFileNameWithoutExtension));
        if (source.Count<string>() > 0)
        {
          int num = 1;
          while (source.Contains<string>(str1))
          {
            str1 = string.IsNullOrEmpty(this.FileName) ? string.Format(this.m_fileNameFormatPattern, (object) this.FileNamePrefix, (object) num) : $"{this.FileName}_{num.ToString()}";
            ++num;
          }
        }
        this.m_fileFullName = string.Format(this.m_fileFormatPattern, (object) path, (object) str1, (object) this.FileFormat);
      }
      else
        this.m_fileFullName = string.Format(this.m_fileFormatPattern, (object) path, (object) str1, (object) this.FileFormat);
    }
    return this.m_fileFullName;
  }

  internal IList<KeyValuePair<string, string>> ScreenCaptureOptions
  {
    get
    {
      this.InitScreenCaptureOptions();
      return this.m_screenCaptureOptions;
    }
  }

  private void InitScreenCaptureOptions()
  {
    if (this.m_screenCaptureOptions != null)
      return;
    this.m_screenCaptureOptions = (IList<KeyValuePair<string, string>>) new List<KeyValuePair<string, string>>();
    this.m_screenCaptureOptions.Add(new KeyValuePair<string, string>("Application", MRCStringTable.SCREEN_CAPTURE_APPLICATION_FRAME));
    if (((IEnumerable<Screen>) Screen.AllScreens).Count<Screen>() > 1)
    {
      Screen[] allScreens = Screen.AllScreens;
      for (int index = 0; index < ((IEnumerable<Screen>) allScreens).Count<Screen>(); ++index)
      {
        if (allScreens[index].Primary)
          this.m_screenCaptureOptions.Add(new KeyValuePair<string, string>(allScreens[index].DeviceName, string.Format(MRCStringTable.SCREEN_CAPTURE_PRIMARY_MONITOR_PATTERN, (object) (index + 1))));
        else
          this.m_screenCaptureOptions.Add(new KeyValuePair<string, string>(allScreens[index].DeviceName, string.Format(MRCStringTable.SCREEN_CAPTURE_MONITOR_PATTERN, (object) (index + 1))));
      }
    }
    this.m_screenCaptureOptions.Add(new KeyValuePair<string, string>("Full Screen", MRCStringTable.SCREEN_CAPTURE_FULL_SCREEN));
  }

  public Rectangle GetScreenCaptureRect()
  {
    Rectangle screenCaptureRect;
    if (this.SelectedScreenCaptureOption == "Application")
    {
      screenCaptureRect = CApMRCRecorderUtilities.GetApplicationRect();
      if (screenCaptureRect.Width % 4 != 0 || screenCaptureRect.Height % 4 != 0)
      {
        int width = screenCaptureRect.Width;
        int height = screenCaptureRect.Height;
        screenCaptureRect = new Rectangle(screenCaptureRect.Left, screenCaptureRect.Top, width % 4 != 0 ? width - width % 4 : width, height % 4 != 0 ? height - height % 4 : height);
      }
    }
    else if (this.SelectedScreenCaptureOption == "Full Screen")
    {
      screenCaptureRect = CApMRCRecorderUtilities.GetFullScreenRect();
    }
    else
    {
      Screen screen = ((IEnumerable<Screen>) Screen.AllScreens).First<Screen>((Func<Screen, bool>) (x => x.DeviceName == this.SelectedScreenCaptureOption));
      screenCaptureRect = screen == null ? new Rectangle() : screen.Bounds;
    }
    return screenCaptureRect;
  }

  public TxVideoCodec Codec { get; set; }

  public TxMovieTimeSource TimeSource { get; set; }

  public bool ChangeSimulationTimeInterval { get; set; }

  public double TimeIntervalAccordingFrameRate
  {
    get
    {
      double accordingFrameRate = 0.0;
      if (this.TimeSource == 1 && this.ChangeSimulationTimeInterval)
        accordingFrameRate = 1000.0 / (double) this.FrameRate / 1000.0;
      return accordingFrameRate;
    }
  }

  public string OverlayText { get; set; }

  public short OverlayFontSize { get; set; }

  public System.Windows.Media.Color OverlayFontColor { get; set; }

  public System.Windows.Media.Color OverlayFontShadowColor { get; set; }

  public string OverlayImagePath { get; set; }

  public uint OverlayImageWidth { get; set; }

  public uint OverlayImageHeight { get; set; }

  public uint OverlayImageSizePercent { get; set; }

  public uint OverlayImageTransparency { get; set; }

  public TxMovieOverlayPosition OverlayPosition { get; set; }

  public void Validate() => CApMRCRecorderUtilities.CheckDefaultSettings(this);

  public System.Drawing.Color ToColor(System.Windows.Media.Color color)
  {
    return System.Drawing.Color.FromArgb((int) color.R, (int) color.G, (int) color.B);
  }
}
